package com.apps.ftapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.MyLessonModel;
import com.apps.ftapp.util.Tools;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by lyli on 2017/10/8.
 */

public class MyLessonAdapter extends BaseAdapter implements View.OnClickListener {


    private Context context;
    private List<MyLessonModel.CourseBean> dataList;
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;

    private MyClickListener mListener;


    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener {
        void clickListener(View v);
    }


    public MyLessonAdapter(Context context, List<MyLessonModel.CourseBean> dataList, MyClickListener listener) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(this.context);
        mListener = listener;

        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }


    public void addRes(List<MyLessonModel.CourseBean> data) {
        if (data != null) {
//            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }


    public void delRes(MyLessonModel.CourseBean data) {
        if (data != null) {
//            this.dataList.clear();
            this.dataList.remove(data);
            notifyDataSetChanged();
        }
    }


    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        final MyLessonModel.CourseBean bean = dataList.get(i);
        ViewHolder viewHolder;
        if (view == null) {
            view = this.layoutInflater.inflate(R.layout.activity_my_lesson_listview_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.lessonTitle.setText(bean.getTitle());


        //
        StringBuffer sb = new StringBuffer();
        if (null != bean.getTeacherIds() && bean.getTeacherIds().size() > 0) {
            for (MyLessonModel.CourseBean.TeacherIdsBean b : bean.getTeacherIds()) {
                sb.append(b.getNickname() + " ");
            }
        }

        viewHolder.lessonLaoshi.setText("上课老师：" + sb.toString());
        viewHolder.lessonTime.setText("上课时间：" + bean.getNew_startTime() + "至" + bean.getNew_endTime());
        if (!bean.getQqqun().equals("")) {
            viewHolder.kefuqq.setText("QQ群：" + bean.getQqqun());
        } else {
            viewHolder.kefuqq.setText("");
        }

        viewHolder.kefuqq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bean.getQqqun().equals("") || bean.getQqqun() == null) {
                    return;
                } else {
                    if (!bean.getQqun_andid().equals("") && bean.getQqun_andid() != null) {
                        joinQQGroup(bean.getQqun_andid());
                    }
                }
            }
        });


        //开课状态（1 为开课中，2 为未开课，3 为已结课）
        if ("1".equals(Tools.isNull(bean.getClassstatus()))) {
            viewHolder.lessonStatus.setBackgroundResource(R.drawable.activity_my_error_subject_red_btn2);
            viewHolder.lessonStatus.setText("开课中");
        } else if ("2".equals(Tools.isNull(bean.getClassstatus()))) {
            viewHolder.lessonStatus.setBackgroundResource(R.drawable.activity_my_error_subject_rec_btn1);
            viewHolder.lessonStatus.setText("未开课");
        } else {
            viewHolder.lessonStatus.setBackgroundResource(R.drawable.activity_my_error_subject_rec_btn1);
            viewHolder.lessonStatus.setText("已结课");
        }


        //coll_img
        if ("1".equals(bean.getMemberfllow())) {
            viewHolder.collImg.setBackgroundResource(R.drawable.f_my_kecheng_fav);
        } else {
            viewHolder.collImg.setBackgroundResource(R.drawable.f_my_kecheng_unfav);
        }
        viewHolder.top_container.setTag(i);
        viewHolder.top_container.setOnClickListener(this);
        viewHolder.delete_lin.setTag(i);
        viewHolder.delete_lin.setOnClickListener(this);
        viewHolder.coll_lin.setTag(i);
        viewHolder.coll_lin.setOnClickListener(this);

//        viewHolder.collImg.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {

        mListener.clickListener(v);
    }


    class ViewHolder {
        TextView lessonStatus;
        TextView lessonTitle;
        TextView lessonLaoshi;
        TextView lessonTime;
        TextView kefuqq;
        ImageView collImg;
        ImageView delImg;
        RelativeLayout top_container;
        RelativeLayout delete_lin;
        RelativeLayout coll_lin;

        public ViewHolder(View view) {
            lessonStatus = view.findViewById(R.id.lesson_status);
            lessonTitle = view.findViewById(R.id.lesson_title);
            lessonLaoshi = view.findViewById(R.id.lesson_laoshi);
            lessonTime = view.findViewById(R.id.lesson_time);
            kefuqq = view.findViewById(R.id.kefuqq);
            collImg = view.findViewById(R.id.coll_img);
            delImg = view.findViewById(R.id.delete_img);
            top_container = view.findViewById(R.id.top_container);
            delete_lin = view.findViewById(R.id.delete_lin);
            coll_lin = view.findViewById(R.id.coll_lin);

        }
    }

    public boolean joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D" + key));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            context.startActivity(intent);
            return true;
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            return false;
        }
    }


}
